<?php

namespace app\applet\controller;

use app\share\model\Token;
use app\share\model\WeChat;
use think\Db;
use think\Validate;

class Asset
{
    public function __construct()
    {
        $this->Asset = new \app\applet\model\Asset();
        $this->we_chat = new WeChat();
        $this->token = new Token();
    }

    public function test(){
        $input = input();
        $member_id = $input['mid'];
        $typeList=Db::name("asset_log_type")->column("te_id,type,atype,name","te_id");
        for($i=0;$i<20;$i++){
            $mt=mt_rand(1,52);
            $type=$typeList[$mt];
            $amount=mt_rand(10,100);
            $source_type=mt_rand(1,2);
            $asset_log=['member_id'=>$member_id,"type"=>$type['type'],"amount"=>$amount,
                'source_type'=>$source_type,"description"=>$type['name'],
                'change_type'=>$type['atype'],'log_type_id'=>$type['te_id'],"create_time"=>time(),"update_time"=>time()];
            Db::name("member_asset_log")->insert($asset_log);
        }
        dump($type);exit;
    }
    /**
     * 获取用户我的账户信息
     */
    public function getAccount(){
        $member_id= get_header_id("token");
        //$member_id=18;
        $data = $this->Asset->getMemberAsset($member_id);
        returnJson(200,'success',$data);
    }

    /**
     * 获取用户冻结积分记录
     */
    public function getFrozenScoreLog(){

        $input = input();
        $where=[];
        $member_id= get_header_id("token");
        $where['member_id']=$member_id;
        $where['type']=3;
        $where['change_type']=1;
        $where['log_type_id']=['in',[47,48]];
        $current_page = empty($input['current_page']) ? 1 : $input['current_page'];
        $page_num = empty($input['page_num']) ? 10: $input['page_num'];
        $data = $this->Asset->getFrozenScoreList($current_page,$page_num,$where);
        returnJson(200,'success',$data);
    }

    /**
     * 获取用户积分记录
     */
    public function getScoreLog(){

        $input = input();
        $input['change_type']=1;
        $validate = new Validate([
            ['type', 'require', '记录类型不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $member_id= get_header_id("token");
        $where=" type=".$input['type']." and change_type=1 and member_id=$member_id ";
        //手机号查询
        if( isset($input['mobile'])&&!empty($input['mobile'])){
            $where.=" and transfer_mobile='".trim($input['mobile'])."'";
        }
        //时间日期查询
        if(isset($input['start_time'])&&isset($input['end_time'])&&!empty($input['start_time'])&&!empty($input['end_time'])){
            $where.=" and create_time>=".$input['start_time']." and create_time<=".$input['end_time'];
        }elseif(isset($input['start_time'])&&!empty($input['start_time'])){//开始时间
            $where.=" and create_time>=".$input['start_time'];
        }elseif(isset($input['end_time'])&&!empty($input['end_time'])){//截止时间
            $where.=" and create_time<=".$input['end_time'];

        }
        //缘由类型查询
        if(isset($input['te_id'])&&!empty($input['te_id'])){
            $where.=" and log_type_id=".$input['te_id'];
        }
        //收卷人
        if(isset($input['source_type'])&&!empty($input['source_type'])){
            $where.=" and source_type=".$input['source_type'];
        }
        $current_page = empty($input['current_page']) ? 1 : $input['current_page'];
        $page_num = empty($input['page_num']) ? 10: $input['page_num'];
        $data = $this->Asset->getScoreList($current_page,$page_num,$where,$input);
        returnJson(200,'success',$data);
    }

    /**
     * 获取用户金币记录
     */
    public function getCoinLog(){
        $input = input();
        $input['change_type']=2;
        $validate = new Validate([
            ['type', 'require', '记录类型不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $member_id= get_header_id("token");
        $where=" type=".$input['type']." and change_type=2 and member_id=$member_id ";

        //手机号查询
        if( isset($input['mobile'])&&!empty($input['mobile'])){
            $where.=" and transfer_mobile='".trim($input['mobile'])."'";
        }
        //时间日期查询
        if(isset($input['start_time'])&&isset($input['end_time'])&&!empty($input['start_time'])&&!empty($input['end_time'])){
            $where.=" and create_time>=".$input['start_time']." and create_time<=".$input['end_time'];
        }elseif(isset($input['start_time'])&&!empty($input['start_time'])){//开始时间
            $where.=" and create_time>=".$input['start_time'];
        }elseif(isset($input['end_time'])&&!empty($input['end_time'])){//截止时间
            $where.=" and create_time<=".$input['end_time'];

        }
        //缘由类型查询
        if(isset($input['te_id'])&&!empty($input['te_id'])){
            $where.=" and log_type_id=".$input['te_id'];
        }
        //收卷人
        if(isset($input['source_type'])&&!empty($input['source_type'])){
            $where.=" and source_type=".$input['source_type'];
        }
        $current_page = empty($input['current_page']) ? 1 : $input['current_page'];
        $page_num = empty($input['page_num']) ? 10: $input['page_num'];
        $data = $this->Asset->getScoreList($current_page,$page_num,$where,$input);
        returnJson(200,'success',$data);
    }


    /**
     * 获取用户微信支付记录
     */
    public function getWxLog(){

        $input = input();
        $input['change_type']=3;
        $member_id= get_header_id("token");
        $where= "change_type=3 and member_id=$member_id ";

        //时间日期查询
        if(isset($input['start_time'])&&isset($input['end_time'])&&!empty($input['start_time'])&&!empty($input['end_time'])){
            $where.=" and create_time>=".$input['start_time']." and create_time<=".$input['end_time'];
        }elseif(isset($input['start_time'])&&!empty($input['start_time'])){//开始时间
            $where.=" and create_time>=".$input['start_time'];
        }elseif(isset($input['end_time'])&&!empty($input['end_time'])){//截止时间
            $where.=" and create_time<=".$input['end_time'];

        }
        //缘由类型查询
        if(isset($input['te_id'])&&!empty($input['te_id'])){
            $where.=" and log_type_id=".$input['te_id'];
        }
        $current_page = empty($input['current_page']) ? 1 : $input['current_page'];
        $page_num = empty($input['page_num']) ? 10: $input['page_num'];
        $data = $this->Asset->getWxpayList($current_page,$page_num,$where,$input);
        returnJson(200,'success',$data);
    }


}